<template>
  <div>
    <el-form :inline="true">
      <el-form-item v-if="hasAuth('permission:add')">
        <el-button type="primary" @click="editPermission(null)">新增权限</el-button>
      </el-form-item>
    </el-form>

    <el-table
        v-if="hasAuth('permission:list')" :data="permissionList"
        row-key="id" border stripe default-expand-all width="100%" :fit="true"
        :tree-props="{children: 'childList', hasChildren: ''}">
      <el-table-column
          prop="resourceName"
          label="权限名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="resourceMark"
          label="权限编码"
          width="200">
      </el-table-column>
      <el-table-column
          prop="resourceIcon"
          label="权限图标"
          width="200">
      </el-table-column>
      <el-table-column
          prop="resourceType"
          label="权限类型"
          width="100">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.resourceType === 'list'">目录</el-tag>
          <el-tag size="small" v-else-if="scope.row.resourceType === 'menu'" type="success">菜单</el-tag>
          <el-tag size="small" v-else-if="scope.row.resourceType === 'button'" type="info">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="resourceUrl"
          label="权限地址"
          width="200">
      </el-table-column>
      <el-table-column
          prop="resourceSeq"
          label="权限序号"
          width="80">
      </el-table-column>
      <el-table-column
          prop="descript"
          label="权限描述"
          width="180"
          show-overflow-tooltip>
      </el-table-column>
      <af-table-column
          fixed="right"
          label="操作">
        <template slot-scope="scope">
          <el-button @click="editPermission(scope.row)" type="text" size="small" v-if="hasAuth('permission:update')">编辑</el-button>
          <el-button @click="" type="text" size="small" v-if="hasAuth('permission:delete')">删除</el-button>
        </template>
      </af-table-column>
    </el-table>

    <!--编辑 新增-->
    <el-dialog
        title="权限信息"
        :visible="editPermissionShow"
        :close-on-click-modal="false"
        width="40%"
        center
        :before-close="editPermissionCardClose">

      <el-form ref="permissionForm" status-icon :rules="permissionRules" :model="permission" label-width="auto">

        <el-form-item label="所属父级" prop="parentId">
          <el-select v-model="permission.parentId" placeholder="请选择上级菜单">
            <template v-for="item in permissionList">
              <el-option :label="item.resourceName" :value="item.id"></el-option>
              <template v-for="child in item.childList">
                <el-option :label="child.resourceName" :value="child.id">
                  <span>{{ "- " + child.resourceName }}</span>
                </el-option>
              </template>
            </template>
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="权限名称" prop="resourceName">
              <el-input v-model="permission.resourceName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="权限类型" prop="resourceType">
              <el-select v-model="permission.resourceType" style="width: 100%">
                <el-option
                    v-for="(item,index) in resourceType" :key="index"
                    :label="item.type" :value="item.type">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="权限编码" prop="resourceMark">
              <el-input v-model="permission.resourceMark"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="权限地址" prop="resourceUrl">
              <el-input v-model="permission.resourceUrl"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="权限图标" prop="resourceIcon">
              <el-input v-model="permission.resourceIcon"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="权限序号" prop="resourceSeq">
              <el-input-number v-model="permission.resourceSeq" :min="1" :max="99"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="权限描述" prop="descript">
          <el-input v-model="permission.descript"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editPermissionCardClose">取 消</el-button>
        <el-button type="primary" @click="okEditPermission">确 定</el-button>
      </span>
    </el-dialog>

    <admin-footer/>
  </div>
</template>

<script>
import {addPermission, getAllPermission, updatePermission} from "@/network/permission";
import AdminFooter from "@/components/conment/adminfooter/AdminFooter";
import {hasPermission} from "@/utils/mixin";
export default {
  name: "Permission",
  components: {AdminFooter},
  mixins:[hasPermission],
  data(){
    return{
      editOrAddFlag:false,
      editPermissionShow:false,
      permissionList:[],//所有权限
      permission:{},
      temPermission:{},
      permissionRules:{
        parentId:[
          {required: true, message: '请选择所属父级', trigger: 'blur' },
        ],
        resourceName:[
          { required: true, message: '请输入权限名称', trigger: 'blur' },
        ],
        resourceType:[
          { required: true, message: '请选择权限类型', trigger: 'blur' },
        ],
        resourceMark:[
          { required: true, message: '请输入权限编码', trigger: 'blur' },
        ],
        resourceSeq:[
          { required: true, message: '请输入权限序号', trigger: 'blur' },
        ],
      },

      resourceType:[
        {type:'list'},
        {type:'menu'},
        {type:'button'},
      ]
    }
  },
  methods:{
    //打开编辑 新增权限界面
    editPermission(row){
      if (row){//编辑
        this.editOrAddFlag = false
        this.permission = JSON.parse(JSON.stringify(row))
        this.temPermission = JSON.parse(JSON.stringify(row))
      }else {//添加
        this.permission = {}
        this.editOrAddFlag = true
      }
      this.editPermissionShow = true
    },
    //关闭新增修改 权限界面
    editPermissionCardClose(){
      this.$refs.permissionForm.clearValidate()
      this.editPermissionShow = false
    },
    //确认 新增修改 权限
    okEditPermission(){
      this.$refs.permissionForm.validate(valid=>{
        if (valid){
          if(this.editOrAddFlag){
            addPermission(this.permission).then(res=>{
              this.$message.success('添加成功')
              this.getAllPermission()
            })
          }else {
            if (JSON.stringify(this.permission) !== JSON.stringify(this.temPermission)){
              this.$delete(this.permission,'childList')
              updatePermission(this.permission).then(res=>{
                this.$message.success('修改成功')
                this.getAllPermission()
              })
            }
          }
          this.editPermissionCardClose()
        }else {
          console.log('表单验证失败')
        }
      })
    },
    //获取所有完整权限
    getAllPermission(){
      getAllPermission().then(res=>{
        this.permissionList = res.data.data
      })
    },
  },
  created() {
    this.getAllPermission()
  }
}
</script>

<style lang="scss">
  .iconList{
    width: 400px;
    height: 300px;
    overflow-y: scroll;//y轴显示滚动条
    overflow-x: hidden;//x轴溢出隐藏
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;//一定要换行
      i{
        display: inline-block;
        width: 60px;
        height: 45px;
        color: #000000;
        font-size: 20px;
        border: 1px solid #E6E6E6;
        border-radius: 4px;
        cursor: pointer;
        text-align: center;
        line-height: 45px;
        margin: 5px ;
        &:hover{
           //color: $ft;
           //border-color: $ft;
         }
      }
  }
</style>